﻿

namespace Common.Algorithm.Core;

public class Rectangle
{
    /// <summary>
    /// 左下角
    /// </summary>
    public Point2d minPnt { get; set; }
    /// <summary>
    /// 右上角
    /// </summary>
    public Point2d maxPnt { get; set; }

    /// <summary>
    /// build
    /// </summary>
    /// <param name="_pnt1"></param>
    /// <param name="_pnt3"></param>
    /// <param name="toCheck"></param>
    public Rectangle(Point2d _pnt1, Point2d _pnt3, bool toCheck = false)
    {
        if (toCheck)
        {
            this.minPnt = new Point2d(Math.Min(_pnt1.X, _pnt3.X), Math.Min(_pnt1.Y, _pnt3.Y));// 
            this.maxPnt = new Point2d(Math.Max(_pnt1.X, _pnt3.X), Math.Max(_pnt1.Y, _pnt3.Y));// 
        }
        else
        {
            this.minPnt = _pnt1;
            this.maxPnt = _pnt3;
        }
    }

    /// <summary>
    /// 判断矩形框是否相交
    /// </summary>
    /// <param name="Extend"></param>
    /// <returns></returns>
    public bool IsIntersect(Rectangle rect)
    {
        if (this.minPnt.X > rect.maxPnt.X || this.minPnt.Y > rect.maxPnt.Y ||
            this.maxPnt.X < rect.minPnt.X || this.maxPnt.Y < rect.minPnt.Y)
        {
            return false;
        }
        return true;
    }

    public Point2d[] GenCornerPnt()
    {
        Point2d[] pnts = new Point2d[4];
        pnts[0] = rigtTopPnt;
        pnts[1] = leftTopPnt;
        pnts[2] = leftBtmPnt;
        pnts[3] = rigtBtmPnt;
        return pnts;
    }

    public IList<Rectangle> SplitRectangle()
    {
        List<Rectangle> rects = new List<Rectangle>();
        rects.Add(new Rectangle(midPnt, rigtTopPnt));
        rects.Add(new Rectangle(leftMidPnt, midTopPnt));
        rects.Add(new Rectangle(leftBtmPnt, midPnt));
        rects.Add(new Rectangle(midBtmPnt, rigtMidPnt));
        return rects;
    }

    /// <summary>
    /// 中心
    /// </summary>
    public Point2d midPnt => (minPnt + maxPnt) * 0.5;

    /// <summary>
    /// 左上
    /// </summary>
    public Point2d leftTopPnt => new Point2d(minPnt.X, maxPnt.Y);
    /// <summary>
    /// 左中
    /// </summary>
    public Point2d leftMidPnt => new Point2d(minPnt.X, midPnt.Y);
    /// <summary>
    /// 左下
    /// </summary>
    public Point2d leftBtmPnt => minPnt;

    /// <summary>
    /// 右上
    /// </summary>
    public Point2d rigtTopPnt => maxPnt;
    /// <summary>
    /// 右中
    /// </summary>
    public Point2d rigtMidPnt => new Point2d(maxPnt.X, midPnt.Y);
    /// <summary>
    /// 右下
    /// </summary>
    public Point2d rigtBtmPnt => new Point2d(maxPnt.X, minPnt.Y);

    /// <summary>
    /// 顶中
    /// </summary>
    public Point2d midTopPnt => new Point2d(midPnt.X, maxPnt.Y);
    /// <summary>
    /// 底中
    /// </summary>
    public Point2d midBtmPnt => new Point2d(midPnt.X, minPnt.Y);

    /// <summary>
    /// 宽度
    /// </summary>
    public double width => Math.Abs((maxPnt - minPnt).X);
    /// <summary>
    /// 高度
    /// </summary>
    public double height => Math.Abs((maxPnt - minPnt).Y);

}

